package com.thingsgrid.basicInterface.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.gson.Gson;
import com.thingsgrid.basicInterface.entity.ApiInterfaceTypeRelation;
import com.thingsgrid.basicInterface.entity.BasicInterface;
import com.thingsgrid.basicInterface.entity.BasicInterfaceType;
import com.thingsgrid.basicInterface.mapper.ApiInterfaceTypeRelationMapper;
import com.thingsgrid.basicInterface.service.IApiInterfaceTypeRelationService;
import com.thingsgrid.basicInterface.service.IBasicInterfaceService;
import com.thingsgrid.basicInterface.service.IBasicInterfaceTypeService;
import com.thingsgrid.basicInterface.vo.TypeChild;
import com.thingsgrid.basicInterface.vo.TypeTree;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class IApiInterfaceTypeRelationServiceImpl extends ServiceImpl<ApiInterfaceTypeRelationMapper, ApiInterfaceTypeRelation> implements IApiInterfaceTypeRelationService {

	@Autowired
	private IBasicInterfaceTypeService basicInterfaceTypeService;

	@Autowired
	private IBasicInterfaceService basicInterfaceService;


	@Override
	public List<Long> getApiIds(Long typeId) {
		List<ApiInterfaceTypeRelation> list = this.list(new LambdaQueryWrapper<ApiInterfaceTypeRelation>()
			.eq(ApiInterfaceTypeRelation::getTypeId, typeId));
		List<Long> apiIds = list.stream().map(e -> e.getApiId()).collect(Collectors.toList());
		return apiIds;
	}

	@Override
	public List<TypeTree> getTypeTrees(List<Long> apiIds) {
		List<BasicInterfaceType> types = basicInterfaceTypeService.list(new LambdaQueryWrapper<BasicInterfaceType>().eq(BasicInterfaceType::getCategory, "0"));
		List<TypeTree> typeTrees = types.stream().map(e -> new TypeTree(e)).collect(Collectors.toList());
		for (TypeTree type : typeTrees){
			List<BasicInterface> apiList = basicInterfaceService.list(new LambdaQueryWrapper<BasicInterface>().eq(BasicInterface::getTypeId, type.getId()));
			List<TypeChild> typeChildren = apiList.stream().map(e -> new TypeChild(e,apiIds)).collect(Collectors.toList());
			type.setChildren(typeChildren);
		}
		return typeTrees;
	}

	@Override // List<TypeTree>
	public String getTypeTreesForAPP(List<Long> apiIds) {
		List<BasicInterfaceType> types = basicInterfaceTypeService.list(new LambdaQueryWrapper<BasicInterfaceType>().eq(BasicInterfaceType::getCategory, "0"));
		List<TypeTree> typeTrees = types.stream().map(e -> new TypeTree(e)).collect(Collectors.toList());
		for (TypeTree type : typeTrees){
			List<BasicInterface> apiList = basicInterfaceService.list(new LambdaQueryWrapper<BasicInterface>().eq(BasicInterface::getTypeId, type.getId()));
			List<TypeChild> typeChildren = apiList.parallelStream().filter(basicInterface -> {
				if (apiIds.contains(basicInterface.getApiId())){
					return false;
				}
				return true;
			}).map(e -> new TypeChild(e)).collect(Collectors.toList());
			//  List<TypeChild> typeChildren = apiList.stream().map(e -> new TypeChild(e,apiIds)).collect(Collectors.toList());
			type.setChildren(typeChildren);
		}

		Gson gson = new Gson();
		String Json = gson.toJson(typeTrees);
		return Json;
	}
}
